
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../main.dart';
import '../widget/app_bar.dart';
import '../util/toast_util.dart';

import 'setting_common_page.dart';
import 'straighten_page.dart';
import 'up_page.dart';


class Setting extends StatefulWidget {
  const Setting({Key? key}) : super(key: key);

  @override
  State<Setting> createState() => _SettingState();
}

class _SettingState extends State<Setting> {

  RxBool isAdmin = RxBool(false);

  var _psw = '';

  var psw = '000000';

  @override
  initState() {
    super.initState();

    if(settingEntity.psw != null && settingEntity.psw != ''){
      psw = settingEntity.psw!;
    }

  }

  _adminLogin(){
    return Container(
        width: 350.w,
        height: 230.h,
        decoration: const BoxDecoration(
            image: DecorationImage(
              fit: BoxFit.fitWidth,
              image: AssetImage(
                "assets/images/admin_bg.png",
              ),
            )
        ),
        alignment: Alignment.center,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Container(
              decoration:BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.all(Radius.circular(16.r)),
                border: Border.all(
                  color: Colors.blue,
                  width: 1,
                ),
              ),
              padding: EdgeInsets.only(left: 10.w,right: 10.w),
              margin:  EdgeInsets.only(left: 30.w, right: 30.w, bottom: 15.h,top: 90.h),
              child: TextField(
                  obscureText:true,
                  minLines: 1,
                  onChanged: (input){
                    _psw = input;
                  },
                  decoration:  InputDecoration(
                    contentPadding: EdgeInsets.only(top: 10.r,bottom: 10.r),
                    isCollapsed: true,
                    border: InputBorder.none,
                    hintText: "请输入登录口令",
                  ),
                  style: TextStyle(color: Colors.black,fontSize: 8.sp)
              )
            ),
            SizedBox(height: 10.h,),
            InkWell(
              onTap: (){
                if(_psw .toUpperCase()== psw){
                  isAdmin.value = true;
                }else{
                  toastWarn('口令错误');
                }
              },
              child:Image.asset("assets/images/admin_login.png",height: 66.h, width: 150.w, fit: BoxFit.fill,),
            ),
          ],
        ),
    );
  }

  _title(){
    return Container(
        margin: EdgeInsets.only(top: 20.h,left: 20.w,right: 20.w),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            InkWell(
              onTap: (){
                Get.back();
              },
              child: Image.asset("assets/images/back.png",width:60.w,height:23.h, fit: BoxFit.fill,),
            ),

            Obx(() => Visibility(visible:isAdmin.value,
                child: InkWell(
                    onTap: (){
                      Get.defaultDialog(
                        title: '退出',
                        titleStyle: TextStyle(
                          color: const Color(0xFF000000),
                          fontSize: 10.sp,
                        ),
                        middleText: '确定退出应用？',
                        middleTextStyle: TextStyle(
                          color: const Color(0xFF000000),
                          fontSize: 8.sp,
                        ),
                        radius: 4.sp,
                        textConfirm: '确定',
                        // textCancel: '取消',
                        confirmTextColor: const Color(0xFF007AFF),
                        backgroundColor: const Color(0xD1F8F8F8),
                        buttonColor: Colors.transparent,
                        onConfirm: () {
                          _showBar();
                          SystemNavigator.pop();
                          exit(0);
                        },
                      );
                    },
                    child:  Text("退出",style: TextStyle(color: Colors.white,fontSize: 12.sp),)
                )
            ))
          ],
        )
    );
  }

  _showBar() async {
    await const MethodChannel('toAndroid').invokeMethod('showBar');
  }

  _image(name,callback){
    return InkWell(
      onTap:(){
        callback();
      },
      child: Image.asset("assets/images/$name.png", width: 360.w, fit: BoxFit.fitWidth,),
    );
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: () => FocusScope.of(context).requestFocus(FocusNode()),
        child: Scaffold(
          resizeToAvoidBottomInset :false,
          body: Container(
            width: double.infinity,
            height: double.infinity,
            decoration: const BoxDecoration(
              image: DecorationImage(
                fit: BoxFit.fill,
                image: AssetImage(
                  "assets/images/bg.png",
                ),
              ),
            ),
            child: Column(
              children: [
                appBar(),
                _title(),
                SizedBox(height: 80.h,),
                Obx(() => Visibility(visible:!isAdmin.value,child: _adminLogin())),
                Obx(() => Visibility(visible:isAdmin.value,child: _image( "setting1",()=>Get.to(()=> const SettingCommon())))),
                SizedBox(height: 10.h,),
                Obx(() => Visibility(visible:isAdmin.value&&(settingEntity.main??false),child: _image( "setting2",()=>Get.to(()=> const Up())))),
                SizedBox(height: 10.h,),
                Obx(() => Visibility(visible:isAdmin.value&&(settingEntity.main??false),child: _image( "setting3",()=>Get.to(()=> const Straighten())))),
                SizedBox(height: 10.h,),
              ],
            ),
          ),
        )
    );
  }

}

